#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct person{
    int age;
    int id;
    char name[20];
};

int main()
{
    person p1;
    p1.age = 20;
    p1.id = 1001;
    strcpy(p1.name, "lili");

    // 1. 写入二进制文件
    ofstream ofs("person.dat", ios::out | ios::binary);
    if(ofs.is_open() == false)
    {
        cout << "open write file failed!" << endl;
        return -1;
    }
    ofs.write((char*)&p1, sizeof(p1));
    ofs.close();

    // 2. 读取二进制文件
    person p2;
    ifstream ifs("person.dat", ios::in | ios::binary);
    if(ifs.is_open() == false)
    {
        cout << "open read file failed!" << endl;
        return -1;
    }
    ifs.read((char*)&p2, sizeof(p2));
    ifs.close();

    cout << p2.age << " " << p2.id << " " << p2.name << endl;

    return 0;
}
